package com.yunji.news.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunji.news.common.Result;
import com.yunji.news.entity.RegistrationEntry;
import com.yunji.news.service.RegistrationEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 报名入口管理控制器
 */
@RestController
@RequestMapping("/admin/registration-entries")
public class AdminRegistrationEntryController {

    @Autowired
    private RegistrationEntryService registrationEntryService;

    /**
     * 分页查询报名入口
     */
    @GetMapping
    public Result<Page<RegistrationEntry>> getRegistrationEntryPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) String schoolName,
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) Integer status) {
        
        Page<RegistrationEntry> page = registrationEntryService.getRegistrationEntryPage(current, size, schoolName, year, status);
        return Result.success("", page);
    }

    /**
     * 根据ID查询报名入口
     */
    @GetMapping("/{id}")
    public Result<RegistrationEntry> getRegistrationEntryById(@PathVariable Long id) {
        RegistrationEntry registrationEntry = registrationEntryService.getById(id);
        if (registrationEntry == null) {
            return Result.error("报名入口不存在");
        }
        return Result.success("", registrationEntry);
    }

    /**
     * 创建报名入口
     */
    @PostMapping
    public Result<String> createRegistrationEntry(@RequestBody RegistrationEntry registrationEntry) {
        boolean success = registrationEntryService.save(registrationEntry);
        if (success) {
            return Result.success("创建成功");
        } else {
            return Result.error("创建失败");
        }
    }

    /**
     * 更新报名入口
     */
    @PutMapping("/{id}")
    public Result<String> updateRegistrationEntry(@PathVariable Long id, @RequestBody RegistrationEntry registrationEntry) {
        registrationEntry.setId(id);
        boolean success = registrationEntryService.updateById(registrationEntry);
        if (success) {
            return Result.success("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    /**
     * 删除报名入口
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteRegistrationEntry(@PathVariable Long id) {
        boolean success = registrationEntryService.removeById(id);
        if (success) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    /**
     * 批量删除报名入口
     */
    @PostMapping("/batch/delete")
    public Result<String> batchDeleteRegistrationEntries(@RequestBody List<Long> ids) {
        boolean success = registrationEntryService.removeByIds(ids);
        if (success) {
            return Result.success("批量删除成功");
        } else {
            return Result.error("批量删除失败");
        }
    }
}